function myRequest(paramsObj, successFun) {
	let sessionId = uni.getStorageSync('sessionid')

	if (sessionId) {
		paramsObj.header = {
			'content-type': 'application/x-www-form-urlencoded',
			'cookie': 'bkbsessionid=' + sessionId
		}
	} else {
		paramsObj.header = {
			'content-type': 'application/x-www-form-urlencoded'
		}
	}
	paramsObj.url = 'http://localhost:8009' + paramsObj.url
	paramsObj.method = 'POST'
	paramsObj.timeout = 5000
	paramsObj.data.channel = 'mini'
	paramsObj.success = successFun
	paramsObj.fail = function() {
		uni.showModal({
			title: '提示',
			content: '网络错误，请求失败'
		})
	}

	uni.request(paramsObj)
}

const baseUrl = 'https://www.zhaobanzhilian.com/api'
//const baseUrl = 'http://localhost:8009/api'
const domain = 'https://www.zhaobanzhilian.com'

function showToastMsg(msg = '', success = () => {}) {
	uni.showToast({
		icon: 'none',
		title: msg,
		success: success
	})
}

const http = (url, data = {}, option = {}) => {
	let hideLoading = option.hideLoading || false,
			loadingTitle = option.loadingTitle || '加载中',
			isCache = option.isCache || false, // 是否从缓存中读数据
			isSetCache = option.isSetCache || false, // 是否缓存数据
			token = {},
			sessionId = uni.getStorageSync('sessionid')
	if (sessionId) {
		token = {
			'content-type': 'application/x-www-form-urlencoded',
			'cookie': 'bkbsessionid=' + sessionId
		}
	} else {
		token = {
			'content-type': 'application/x-www-form-urlencoded'
		}
	}

	if (!hideLoading) {
		uni.showLoading({
			title: loadingTitle,
			mask: true
		})
	}
	
	let tmpData = JSON.parse(JSON.stringify(data))
	tmpData.url = url
	let cacheKey = JSON.stringify(tmpData)
	let cacheData = uni.getStorageSync(cacheKey)
	if (isCache && cacheData) {
		return cacheData
	}
	
	data.channel = 'mini'
	
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method: option.method || 'POST',
			header: token,
			data: data,
			success: res => {
				if (!hideLoading) {
					uni.hideLoading()
				}
				
				if (res.data && res.data.data && res.data.data.sessionId) {
					uni.setStorageSync('sessionid', res.data.data.sessionId)
				}
				
				if (res.data.code == 20002) { // 未注册，去注册
					return uni.redirectTo({
						url: '/pages/register_select/register_select'
					})
				}
				
				if (res.data.code == 20010) { // 服务器重启导致原先登录异常
					return uni.redirectTo({
						url: '/pages/login/login'
					})
				}
				
				if (res.data.code == 20001) {
					uni.removeStorageSync('userInfo')
				}
				
				if (res.data.code != 20000) { // 返回异常，提示一下
					resolve(res.data)
					return showToastMsg(res.data.message)
				}
				
				if (res.data.code == 20000) {
					if (isSetCache) {
						uni.setStorageSync(cacheKey, res.data)
					}
				}
				
				return resolve(res.data)
			},
			
			fail: (err) => {
				if (!hideLoading) uni.hideLoading()
				return uni.showModal({
					title: '提示',
					content: "网络错误，请检查网络"
				})
			}
		})
	})
}

const getTimeStr = (s) => {
	let data = new Date().toLocaleDateString()
	let dd = Date.parse(data) / 1000
	let iday = Math.floor(parseInt(dd - s) / 60 / 60 / 24)
	let d = new Date(s * 1000)
	let ddd = d.toLocaleDateString().split('/')
	let h = d.getHours()
	let m = d.getMinutes()
	let dayType = ''
	let ho = 0
	let mi = ''
	if (h >= 0 && h < 6) {
		dayType = '凌晨'
	} else if (h >= 6 && h < 12) {
		dayType = '上午'
	} else if (h >= 12 && h < 13) {
		dayType = '中午'
	} else if (h >= 13 && h < 18) {
		dayType = '下午'
	} else {
		dayType = '晚上'
	}
	if (h > 12) {
		ho = h - 12
	}
	if (m < 10) {
		mi = '0' + m
	} else {
		mi = m
	}
	if(iday == -1){
	    return dayType + ho + ':' + mi
	}else if(iday == 0){
	    return '昨天' + dayType + ho + ':' + mi
	}else{
	    return ddd[1] + '月' + ddd[2] + '日 ' + dayType + ho + ':' + mi
	}
}

export {
	myRequest,
	showToastMsg,
	http,
	domain,
	getTimeStr
}
